package com.wyx.rtddels.util;

import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import cn.hutool.json.JSONUtil;
import com.alibaba.excel.EasyExcel;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.io.File;

/**
 * @author WYX
 * @date 2024/8/21
 * @Description
 */
@Component
public class HttpUtils {

    //传递过来的auth 认证
    @Value("${rtd.auth}")
    private  String auth;

    /**
     * 测试http请求
     * @param url 传递过来的url 请求
     * @param object 传递过来的json 字符串  纯 post 请求
     */
    public String testHttpservlet(String url,Object object) {

        // "http://128.128.10.101:8090/rtd/tag/remove"
        HttpResponse response = HttpRequest.
                post(url)
                .contentType("application/json")
                //写入token值
                .header("Authorization", auth)
                //传递的json 字符串 写入需要传递的json 串
                .body(JSONUtil.toJsonStr(object))
                //  写入传递的参数值
                .execute();
        String body = response.body();
        //拿取结果值
        // String code = (String)JSONUtil.parseObj(body).get("code");
        return body;
    }

    public String getOut(String url,Object object,String fileName) {

        // "http://128.128.10.101:8090/rtd/tag/remove"
        HttpResponse response = HttpRequest.
                post(url)
                // post("http://localhost:8080/testExport/1")
                .contentType("application/json")
                //写入token值
                .header("Authorization", auth)
                //传递的json 字符串 写入需要传递的json 串
                .body(JSONUtil.toJsonStr(object))
                //  写入传递的参数值
                .execute();
        response.writeBodyForFile(new File("E:\\file_on\\"+fileName),null);
        //拿取结果值
        // String code = (String)JSONUtil.parseObj(body).get("code");
        return "yes";
    }
}
